﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Medical.Events;
using System.ComponentModel;
using Sector7.Framework.Utility;
using System.ServiceModel;
using Sector7.Medical.InterviewStation.Services;

namespace Sector7.Medical.InterviewStation.Windows
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MainWindowBase
    {
        public override Grid GetDesk()
        {
            return grdDesk;
        }

        public override void LoadConfiguration()
        {
            base.LoadConfiguration();

            navigationStrip.InitializeControl(Senvironments.NavigationConfigList);
            footerStrip.InitializeControl();

            OnLoadConfigurationCompleted(new EventArgs());
        }

        private void ExitSystem()
        {
            MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "退出系统？");

            if (result == MessageBoxResult.Yes)
            {
                this.Close();
                Environment.Exit(0);
            }
        }

        private void LoginControl_ClickClose(object sender, EventArgs e)
        {
            ExitSystem();
        }

        private void HeaderStrip_ClickClose(object sender, EventArgs e)
        {
            ExitSystem();
        }

        private void MainWindowBase_Initialized(object sender, EventArgs e)
        {
            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

            worker.RunWorkerAsync();
        }

        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            using (ServiceHost host = new ServiceHost(typeof(InterviewService)))
            {
                host.Opened += delegate
                {
                    Console.WriteLine("InterviewService has been started!");
                };
                host.Open();

                Console.Read();
            }
        }

        private void LoginControl_LoginSuccess(object sender, EventArgs e)
        {
            this.Content = grdMain;
        }

        private void NavigationStrip_ClickNavigationItem(object sender, ClickNavigationItemEventArgs e)
        {
            var navigationConfig = e.NavigationConfig;
            var moduleConfig = Senvironments.ModuleConfigList.Find(delegate(ModuleConfiguration __moduleConfig)
            {
                return __moduleConfig.ModuleId == navigationConfig.ModuleId;
            });

            if (moduleConfig != null)
            {
                string modulePath = PathUtil.Combine(moduleConfig.Path);
                UserControl module = ReflectionUtil.CreateInstance(modulePath, navigationConfig.ClassName) as UserControl;

                if (module != null)
                {
                    headerStrip.UpdateTitle(navigationConfig.Caption);
                    LoadModule(module);
                }
                else
                {
                    Alert.ShowDialog(AlertType.Error, "加载{0}模块失败！", moduleConfig.ModuleId);
                }
            }
            else
            {
                Alert.ShowDialog(AlertType.Error, "无法获取{0}模块信息！", navigationConfig.ModuleId);
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            var loginControl = ReflectionUtil.CreateInstance(
                PathUtil.Combine(@"Modules\SystemManagement.dll"),
                "Sector7.Medical.SYSM.Controls.LoginControl") as LoginControlBase;
            loginControl.LoginSuccess += new EventHandler(LoginControl_LoginSuccess);
            loginControl.ClickClose += new EventHandler(LoginControl_ClickClose);

            this.Content = loginControl;
        }
    }
}
